﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace WL_WinForm
{
    public partial class FrmPurview : DockContent
    {
        public FrmPurview()
        {
            InitializeComponent();
            BindData();
        }

        private void BindData()
        {
            WLong.BLL.Purview Purview = new WLong.BLL.Purview();
            gvPurview.DataSource = Purview.GetList("Disabled=0").Tables[0];
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtPurviewGrade .Text =="")
            {
                COMMON.ControlCommon.ShowMessageBoxError("权限级别为空!");
                return;
            }
            WLong.BLL.Purview purview = new WLong.BLL.Purview();
            if (txtPurviewID .Text != "")
            {
                WLong.Model.Purview mPurview = new WLong.Model.Purview();
                mPurview.PurviewID = new Guid(txtPurviewID.Text);
                mPurview.PurviewGrade = txtPurviewGrade.Text.Trim();
                mPurview.Disabled = 0;
                mPurview.REC_ModifyBy = COMMON.Login.loginName;
                mPurview.REC_ModifyTime = DateTime.Now;

                if (purview.Update(mPurview))
                {
                    ClearControl();
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("修改成功！");
                }
            }
            else
            {
                WLong.Model.Purview mPurview = new WLong.Model.Purview();
                mPurview.PurviewID = Guid.NewGuid();
                mPurview.PurviewGrade = txtPurviewGrade.Text.Trim();
                mPurview.Disabled = 0;
                mPurview.REC_CreateBy = COMMON.Login.loginName;
                mPurview.REC_CreateTime = DateTime.Now;

                purview.Add(mPurview);
                ClearControl();
                BindData();
            }
        }

        private void ClearControl()
        {
            txtPurviewGrade.Text = "";
            txtPurviewID.Text = "";
        }

        private void gvPurview_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtPurviewID.Text = gvPurview.CurrentRow.Cells["PurviewID"].Value.ToString();
            txtPurviewGrade.Text = gvPurview.CurrentRow.Cells["PurviewGrade"].Value.ToString();
        }

        private void gvPurview_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                WLong.BLL.Purview purview = new WLong.BLL.Purview();
                WLong.Model.Purview mPurview = new WLong.Model.Purview();
                mPurview.PurviewID = new Guid(gvPurview.CurrentRow.Cells["PurviewID"].Value.ToString());
                mPurview.PurviewGrade = gvPurview.CurrentRow.Cells["PurviewGrade"].Value.ToString();
                mPurview.Disabled = 1;
                mPurview.REC_ModifyBy = COMMON.Login.loginName;
                mPurview.REC_ModifyTime = DateTime.Now;
                if (purview.Update(mPurview))
                {
                    BindData();
                    COMMON.ControlCommon.ShowMessageBoxOk("删除成功！");
                }
                else
                {
                    COMMON.ControlCommon.ShowMessageBoxError("删除失败！");
                }
            }
        }
    }
}
